<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../VueJs/vue.js"></script>
</head>

<body>
    <div id="root">
        <h1>今天的天气很{{info}}</h1>
        <button @click="switchWeather">天气切换</button>
    </div>
</body>
<script>
    Vue.config.productionTip = false;
    Vue.config.devtools = true;
    let flag = true;
    const vm = new Vue({
        el: '#root',
        data: {
            isHot: true,
        },
        methods: {
            switchWeather() {
                this.isHot = !this.isHot;
            }
        },
        computed: {
            info() {
                return this.isHot ? "炎热" : '凉爽';
            }
        },
        watch: {
            //正常写法
            /*   isHot: {
                  handler: function (newValue, oldValue) {
                      console.log(`isHot被改变了 新的值为${newValue} 旧的值为${oldValue}`);
                  }
              } */

            //配置项只有handler时可简写,还可触发对象方法的简写形式
            isHot: function (newValue, oldValue) {
                console.log(`isHot被改变了 新的值为${newValue} 旧的值为${oldValue}`);
            }

        }
    });
    //正常写法
    /* vm.$watch('isHot', {
        immediate: true,
        handler(newValue, oldValue) {
            console.log('isHot被修改了', newValue, oldValue)
        }
    }) */

    //简写，配置项只有handler时可简写
    //this为vm(vue实例)
    vm.$watch('isHot', function (newValue, oldValue) {
        console.log('isHot被修改了', newValue, oldValue, this);
    });
    //this为window
    vm.$watch('isHot', (newValue, oldValue) => {
        console.log('isHot被修改了', newValue, oldValue, this);
    });
    //被vue所管理的函数尽量使用普通函数，少用箭头函数可以避免this指向问题
</script>

</html>